$("#fh").click(function () {
        // window.location = '../../../login.html';
        layer.msg("返回上一个页面")
})
$("#sx").click(function () {
    window.location = 'backgroundManagementPersonnelList.html';
})

function  backmalist(){
    backmalisttableI.reload('backmalisttableId');
}
var backmalisttableI = null;
var table = null;
layui.use(['table', 'laydate','form'], function(){
        var
        laydate = layui.laydate,
        form = layui.form,
        data = null;
        table = layui.table
        laydate.render({
                elem: '#rq', //指定元素
        });
    backmalisttableI = table.render({
                elem: '#test'
                ,url:'/backgroundmanagementpersonnellist/getAll'
                ,toolbar:'#toolbarDemo'
                ,id:'backmalisttableId'

                ,page:{
                        limit:3,
                        limits:[3,6,9],
                        layout:['count','prev','page','next','limit'],
                        groups:2
                    }
                ,cols: [
                    [
                          {type: 'checkbox'}
                        ,{field:'userId', width:80, title: '序号', sort: true,align:'center'}
                        ,{field:'userNickname', width:90, title: '用户昵称',align:'center'}
                        ,{field:'userUrl', width:150, title: '照片',templet:function (d){
                            return '<img src="http://43.143.176.39/'+d.userUrl+'" width="100px" height="90px">'
                        }}
                        ,{field:'userSex', width:70,align:'center', title: '性别',templet: function(row){
                            if(row.userSex == '1'){
                                return '男';
                            }else if(row.userSex == '0'){
                               return '女';
                            }
                        }}
                        ,{field:'userAccount',align:'center', width:130, title: '账号',height: 500}
                        ,{field:'deptName', width:100, title: '所属部门',templet: function(row){
                            return row.department.deptName;
                        }}

                        ,{field:'userEnableStatus', title: '是否启用', width: 110,templet: function(row){
                                    var str = '<a lay-event="required">';
                                    if(row.userEnableStatus == '0'){
                                        str += '<input type="checkbox" name="deptState" checked lay-skin="switch" lay-text="启用|不启用">';
                                    }else if(row.userEnableStatus == '1'){
                                        str += '<input type="checkbox" name="deptState" lay-skin="switch" lay-text="启用|不启用">';
                                    }
                                    str += '</a>';
                                    return str;
                                }
                        },
                        {field:'userStats', title: '帐号状态', width: 110,templet: function(row){
                            var str = '<a lay-event="required2">';
                            if(row.userStats == '1'){
                                str += '<input type="checkbox" name="deptState" checked lay-skin="switch" lay-text="使用中|停封中">';
                            }else{
                                str += '<input type="checkbox" name="deptState" lay-skin="switch" lay-text="使用中|停封中">';
                            }
                            str += '</a>';
                            return str;
                        }
                    }
                        ,{field:'userCreateDate', title: '注册时间',  width: 160}
                        ,{fixed: '', title:'操作', minWidth: 80,toolbar: '#barDemo'}
                ]
                ],
                skin: 'line',
                parseData:function (res){
                        return{
                                "code":res.code, // 解析接口状态
                                "msg": res.msg, // 解析提示文本
                                "count": res.count, // 解析总条数
                                "data": res.data // 解析数据列表
                        };
                }
        });

        table.on('tool(test)', function(obj){ // 双击 toolDouble
                data = obj.data;
                console.log(data)
                if(obj.event === 'ck'){
                    localStorage.setItem("userId",data.userId);
                            layer.open({
                                type:2,
                                title:'查看用户信息',
                                content:'backgroundManagementPersonnelListSee.html',
                                area: ['1200px', '630px'],
                                offset: ['80px', '240px'],

                            })

                } else if(obj.event === 'qx'){
                    localStorage.setItem("deptName",data.department.deptName);
                    layer.open({
                        type:2,
                        title:'权限设置',
                        content:'backgroundManagementPersonnelListQx.html',
                        area: ['750px', '500px'],
                        // offset: ['200px', '250px'],
                        success:function (layero,index) {
                            var bo = layer.getChildFrame('body', index);
                            bo.find('span[id="tb22"]').html(data.userNickname);
                        }
                    })
                }else if(obj.event === 'bj'){
                    localStorage.setItem("userId",data.userId);
                    layer.open({
                        type:2,
                        title:'编辑用户信息',
                        content:'backgroundManagementPersonnelListBj.html',
                        area: ['1200px', '630px'],
                        offset: ['80px', '240px']
                    })
                } else if(obj.event === 'required'){
                    // alert("序号:"+data.deptId+"是否启用:"+data.deptState);
                       switch (data.userEnableStatus) {
                           case 0:
                               qy(1)
                               break;
                           case 1:
                               qy(0)
                               break;
                    }
                }else if(obj.event === 'required2'){
                    // alert("序号:"+data.deptId+"是否启用:"+data.deptState);
                    switch (data.userStats) {
                        case 1:
                            qy2(2)
                            break;
                        case 2:
                            qy2(1)
                            break;
                        case 0:
                            qy2(2)
                    }
                }else if(obj.event === 'ft'){
                    layer.open({
                        type:2,
                        title:'封停后台用户',
                        content:'backgroundManagementPersonnelListFt.html',
                        area:['600px','430px'],
                        offset: ['230px', '500px'],
                        success:function (layero,index){
                            var bo = layer.getChildFrame('body',index);
                            bo.find('input[name="userId"]').val(data.userId);
                            bo.find('input[name="userAccount"]').val(data.userAccount);

                        }
                    })
                }


        });

        form.on('submit(go)',function (d){

                d = d.field;
                // console.log(d);
                table.reload('backmalisttableId',{
                        where:{
                            userAccount:d.userAccount
                            ,userNickname:d.userNickname
                            ,deptName:d.deptName
                            ,userCreateDate:d.userCreateDate
                        }
                });
        })
    function qy(sta) {
        $.ajax({
            type:"post",
            url:'/backgroundmanagementpersonnellist/updateStatus',
            data: {"userId":data.userId,"userEnableStatus":sta},
            dataType:'json',
            success:function (res) {
                if (res.state == 200){
                    layer.msg(res.msg,{time:500,icon:6})
                }
                else {
                    layer.msg(res.msg,{time:500,icon:5})
                }
            },
            error:function(e){
                console.log("异常");

            }

        })
    }
    function qy2(sta2) {
        $.ajax({
            type:"post",
            url:'/backgroundmanagementpersonnellist/updateStats',
            data: {"userId":data.userId,"userStats":sta2},
            dataType:'json',
            success:function (res) {
                if (res.state == 200){
                    layer.msg(res.msg,{time:500,icon:6})
                }
                else {
                    layer.msg("失败",{time:500,icon:5})
                }
            },
            error:function(e){
                console.log("异常");

            }

        })
    }
});
// ztree树展示
$(document).ready(function () {
    $.ajax({
        type: "get",
        url: "/backgroundmanagementpersonnellist/tree",
        async: true,
        success: function (res) {
            zTreeObj = $.fn.zTree.init($("#bmptree"), settingss, res); //初始化树
            // console.log(res.Product);
            zTreeObj.expandAll(true);   //true 节点全部展开、false节点收缩
        }
    });
});
var settingss = {
    data: {
        simpleData: {
            enable: true,  //true 、 false 分别表示 使用 、 不使用 简单数据模式
            idKey: "deptId",   //节点数据中保存唯一标识的属性名称
            pIdKey: "deptParentId",    //节点数据中保存其父节点唯一标识的属性名称
            rootPId: 0  //用于修正根节点父节点数据，即 pIdKey 指定的属性值
        },
        key: {
            name: "deptName"  //zTree 节点数据保存节点名称的属性名称  默认值："name"
        }
    },

    callback: {
        onClick: onClick
    }

};

function onClick(event, treeId, treeNode) {
    if(treeNode.deptParentId == 0){
        table.reload('backmalisttableId',{
            where:{
                aa:null
            }
        });
    }else {
        // console.log(treeNode.deptId);
        table.reload('backmalisttableId',{
            where:{
                aa:treeNode.deptId
            }
        });
    }
}




